package com.example.myviewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

/**
 * author: ts
 * created on: 2021/8/28 下午7:04
 * description:
 */
public class AddFoldView extends FlexBoxLayoutViewGroup{
    //上拉或下拉显示图标
    private View mUpFoldView;
    private View mDownFoldView;

    public void setIsFold(boolean isFold) {
        mIsFold = isFold;
    }

    public AddFoldView(Context context) {
        super(context);
    }

    public AddFoldView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mUpFoldView = LayoutInflater.from(context).inflate(R.layout.view_item_fold_up, null);
        mDownFoldView = LayoutInflater.from(context).inflate(R.layout.view_item_fold_down, null);
        Log.d("TAG", "FlexBoxLayoutViewGroup: ");

        mUpFoldView.setOnClickListener(view -> {
            mIsFold = true;
            mMyBaseAdapter.notifyDataChanged();
        });

        mDownFoldView.setOnClickListener(view -> {
            mIsFold = false;
            mMyBaseAdapter.notifyDataChanged();
        });

        setOnFoldChangedListener((canFold, fold, index, surplusWidth) -> {
            if (canFold) {
                MyUtil.removeFromParent(mUpFoldView);
                addView(mUpFoldView);
                Log.d("TAG", "onFoldChange: canFold " + index);
                if (fold) {
                    Log.d("TAG", "onFoldChange: fold");
                    MyUtil.removeFromParent(mDownFoldView);
                    int indexValue = indexValue(index, surplusWidth);
                    addView(mDownFoldView, indexValue);
                } else {
                    Log.d("TAG", "onFoldChange: unFold");
                    MyUtil.removeFromParent(mUpFoldView);
                    if (!mMyBaseAdapter.mIsDelete){
                        addView(mUpFoldView);
                    }
                }
            }
        });
    }

    public int indexValue(int index, int surplusWidth) {
        int indexValue;
        int indexWidth = MyUtil.getViewWidth(mDownFoldView);
        if (surplusWidth >= indexWidth) {
            indexValue = index + 1;
        } else {
            indexValue = index;
        }
        return indexValue;
    }
}
